package com.century.sjt.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.Forget_pwdPayActivity;
import com.century.sjt.util.Base64Utils;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.gridpasswordview.GridPasswordView;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description: 支付旧密码
 * @date : 2016/8/19 17:19
 */
public class Setting_oldpwdPayFragment extends Fragment {
    private LinearLayout Old_Layout;
    private GridPasswordView old_payPwd;
    private String oldPwd;
    private TextView Forget_Pwd;
    private RequestQueue mQueue;


    public interface oldClickListener {
        void onOldClick(String oldPwd, int index,String sn);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sjt_modify_paypws_fragment, null);
        old_payPwd = (GridPasswordView) view.findViewById(R.id.paypwd_old);
        Forget_Pwd = (TextView) view.findViewById(R.id.text_wangjiMM);
        mQueue = Volley.newRequestQueue(getActivity());
        initEvent();
        return view;
    }

    private void initEvent() {
        old_payPwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onChanged(String psw) {

            }

            @Override
            public void onMaxLength(String psw) {
                if (!(psw.length() == 6)) {
                    TipUtil.showToast(getResources().getString(R.string.error_is_no6_old_password), getActivity(), 1);
                } else {
                    byte[] encodePwd = old_payPwd.getPassWord().getBytes();
                    oldPwd = Base64Utils.encode(encodePwd);
                    checkCashPwd();
                }


            }
        });

        Forget_Pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("setpwd", "1");
                intent.setClass(getActivity(), Forget_pwdPayActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * @Description: 校验支付密码
     * @author: 周玲波
     * @date: 2016/9/5 21:33
     */
    private void checkCashPwd() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), getActivity());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.CheckCashPwd, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    Log.d("code", code);
                    String message = res.getString("message");
                    if (code.equals("200")) {
                        JSONObject data = res.getJSONObject("data");
                        SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = mainSharedPreferences.edit();
                        editor.putString("isCashPwdSet", "1");
                        editor.commit();
                        if (data.getString("result").equals("1")) {
                            if (getActivity() instanceof oldClickListener) {
                                ((oldClickListener) getActivity()).onOldClick(oldPwd, 2, data.getString("sn"));
                            }
                        }else {
                            TipUtil.showToast(getResources().getString(R.string.error_pwd_unlikeliness), getActivity(), 1);
                        }

                    } else {
                        TipUtil.showToast(message, getActivity(), 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_pwd), getActivity(), 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), getActivity(), 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("password", oldPwd.trim());
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }


}
